local DIY = require "packages/diy_utility/diy_utility"
local shetu = fk.CreateSkill{
  name = "steam__shetu",
  tags = { DIY.ReadySkill}
}

Fk:loadTranslationTable{
  ["steam__shetu"] = "涉途",
  [":steam__shetu"] = "蓄势技，回合结束时，若本回合你使用了至少三张牌且多于上一回合，你可以执行一个额外的回合。",
  ["@steam__shetu_last_used"] = "已涉途",

  ["$steam__shetu1"] = "大军渡江，昼夜驰上！",
  ["$steam__shetu2"] = "白衣摇橹，昼夜兼行！",
}

shetu:addEffect(fk.TurnEnd, {
  can_trigger = function(self, event, target, player, data)
    if target ~= player then return false end
    if not player:hasSkill(self) then return false end
    local room=player.room
    -- 获取本回合使用的牌数
    local num = 0
    player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use and use.from == player then
        num = num + 1
      end
    end, Player.HistoryTurn)
    
    -- 获取上一回合使用的牌数
    local last_used = 0
    local turn_event = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    local turn_event_id = turn_event and turn_event.id or room.logic:getCurrentEvent().id
    local turn_events = room.logic:getEventsByRule(GameEvent.Turn, 1, function (e)
      return e.id < turn_event_id and e.data.who == player
    end)
    if #turn_events > 0 then
      room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        if e.id > turn_events[1].id and e.id < turn_events[1].end_id then
          local use = e.data
          if use and use.from == player then
            last_used = last_used + 1
          end

        end
      end, turn_events[1].id)
    end
   --无论如何记录一下吧
   room:setPlayerMark(player, "@steam__shetu_last_used", num)

    if num >= 3 and num > last_used then
      return true
    
    end
    
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:gainAnExtraTurn(true, self.name)
  end,
})


shetu:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_refresh=function(self, event, target, player, data)
    if target ~= player then return false end
    if not player:hasSkill(self) then return false end
    local room=player.room
    room:setPlayerMark(player, "@steam__shetu_last_used", player:getMark("steam__shetu_this_used"))--现在是上回合的了
   
    
    -- 获取本回合使用的牌数
    local num = 0
    player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data
      if use and use.from == target then
        num = num + 1
      end
    end, Player.HistoryTurn)
  
    room:setPlayerMark(player, "steam__shetu_this_used", num)--无论如何记录一下吧
  end,

})


return shetu 